package com.smartfast4j.backend.modules.sys.security.shiro.filter;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Map;

/**
 * Created by gacl on 2017/9/9.
 */
public class ShiroFilterUtils {

    private static final Logger logger = Logger.getLogger(ShiroFilterUtils.class);

    /**
     * 是否是Ajax请求
     * @param request
     * @return
     */
    public static boolean isAjax(ServletRequest request){
        String ajaxRequest = ((HttpServletRequest) request).getParameter("ajaxRequest");
        logger.info("ajaxRequest="+ajaxRequest);
        return "yes".equalsIgnoreCase(ajaxRequest);
    }

    /**
     * 写出数据
     *
     * @param res 输出的字符串
     * @throws Exception
     */
    protected static void out(ServletResponse servletResponse,String res) {
        Writer writer = null;
        try {
            HttpServletResponse httpServletResponse = ((HttpServletResponse)servletResponse);
            res = (null == res ? "" : res);
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
            writer = httpServletResponse.getWriter();
            logger.debug("输出JSON字符串：" + res);
            writer.write(res);
        } catch (IOException e) {
            logger.error("输出JSON字符串异常", e);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }

    /**
     * 写出数据
     *
     * @param res 输出的字符串
     * @throws Exception
     */
    protected static void out(HttpServletResponse httpServletResponse,String res) {
        Writer writer = null;
        try {
            res = (null == res ? "" : res);
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
            writer = httpServletResponse.getWriter();
            logger.debug("输出JSON字符串：" + res);
            writer.write(res);
        } catch (IOException e) {
            logger.error("输出JSON字符串异常", e);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }

    /**
     *
     * @param servletResponse
     * @throws IOException
     */
    public static void out(ServletResponse servletResponse){
        HttpServletResponse httpServletResponse = ((HttpServletResponse)servletResponse);
        httpServletResponse.setHeader("SessionStatus", "timeout");//在响应头设置session状态
    }

    /**
     * response 输出JSON
     * @param servletResponse
     * @param resultMap
     * @throws IOException
     */
    public static void out(ServletResponse servletResponse, Map<String, Object> resultMap){
        PrintWriter out = null;
        try {
            HttpServletResponse httpServletResponse = ((HttpServletResponse)servletResponse);
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
            out = httpServletResponse.getWriter();
            out.println(JSONObject.toJSONString(resultMap));
        } catch (Exception e) {
            logger.error("输出JSON字符串异常", e);
        }finally{
            if(null != out){
                out.flush();
                out.close();
            }
        }
    }

    /**
     * response 输出JSON
     * @param httpServletResponse
     * @param resultMap
     * @throws IOException
     */
    public static void out(HttpServletResponse httpServletResponse, Map<String, Object> resultMap){
        PrintWriter out = null;
        try {
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
            out = httpServletResponse.getWriter();
            out.println(JSONObject.toJSONString(resultMap));
        } catch (Exception e) {
            logger.error("输出JSON字符串异常", e);
        }finally{
            if(null != out){
                out.flush();
                out.close();
            }
        }
    }
}
